package com.gz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gz.common.OrderService;
import com.gz.common.UserService;
import com.gz.common.model.Order;
import com.gz.common.model.OrderProduct;
import com.gz.common.model.User;
import com.gz.utils.Response;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

/**
 * @author ZhangZhanyi
 * 订单记录
 */
public class OrderController extends Controller{
    
	/**
	 * @author ZhangZhanyi
     * @param pageNum 当前页
     * @param pageSize 显示条数
     * @param order_status 订单状态
     * @param userId 用户id
     * @param payment_status 支付状态
     * @param shipping_status 配送状态
     * @param active 删除标记
     *  订单记录查询
     * http://localhost:8081/cms-admin/order/findOrderListAll
     */
	public void findOrderListAll() {
		Integer pageNum=getParaToInt("pageNum");
		Integer pageSize=getParaToInt("pageSize");
		Integer order_status=getParaToInt("order_status");
		Integer userId=getParaToInt("userId");
		Integer payment_status=getParaToInt("payment_status");
		Integer shipping_status=getParaToInt("shipping_status");
		boolean active=getParaToBoolean("active");
		Page<Order>page=OrderService.getService().getOrder(pageNum, pageSize, order_status, userId, payment_status, shipping_status, active);
		renderJson(Response.responseJson(1001, "请求成功",page));
	}
	
	/**
	 * @author ZhangZhanyi
	 * @param orderid 订单id
	 * 根据订单id查询当前订单所有商品    规格  名字价格
	 * http://localhost:8081/cms-admin/order/findOrderProducts
	 */
	public void findOrderProducts() {
		Integer orderid=getParaToInt("orderid");
	    renderJson(Response.responseJson(1001, "请求成功",OrderService.getService().getOrderProductList(orderid)));
	}
	
	/**
	 * @author ZhangZhanyi
	 * @param orderid 订单id
	 * @param order_status 订单状态  1待付款；2待发货;3已发货 4已完成 5申请退款中6退款成功7已取消;,8申请售后
	 * @param shipping_status 配送状态 0未发货；1已发货；2已收货；3退货中；4已退货
	 * @param usergrade 当前用户身份  
	 *  修改订单订单状态或者配送状态
	 *  http://localhost:8081/cms-admin/order/MvOrder
	 */
	public void MvOrder() {
		Integer orderid=getParaToInt("orderid");
		Integer order_status=getParaToInt("order_status");
		Integer shipping_status=getParaToInt("shipping_status");
		Integer usergrade=getParaToInt("usergrade");
		
		//根据id查询到对象
		Order order=OrderService.getService().findOrderByid(orderid);
		if (order_status!=null) {
			order.setOrderStatus(order_status);
		}else if(shipping_status!=null){
			order.setShippingStatus(shipping_status);
		}
		order.update();
		renderJson(Response.responseJson(1001, "请求成功",true));
	}
	
	/**
	 * 首页查询销量排行
	 * http://localhost:8081/cms-admin/order/findorOrderll
	 */
	public void findorOrderll() {
		List<Order> orders = OrderService.getService().findorOrderll();
		renderJson(Response.responseJson(1001, "请求成功",orders));
	}
	
	/**
	 * @author ZhangZhanyi
     * @param pageNum 当前页
     * @param pageSize 显示条数
     * @param userId 用户id
     * 查询用户已付款订单 并统计条数
     * @return 如果用户是总分销商返回 map否则放回null
     * http://localhost:8081/cms-admin/order/findPaymentOrdersByid
	 */
	public void findPaymentOrdersByid() {
		Integer userId=getParaToInt("userId");
		Integer pageNum=getParaToInt("pageNum");
		Integer pageSize=getParaToInt("pageSize");
		//根据idc查询对象
		User us=UserService.getService().findUserId(userId);
		//如果用户为总分销商则返回数据 
		if (us.getUsergrade()==3) {
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("Count", OrderService.getService().CountPaymentOrders(userId));
			map.put("Payment",OrderService.getService().PaymentOrders(pageNum, pageSize, userId));
			renderJson(Response.responseJson(1001, "请求成功", map));
		}else {
			renderJson(Response.responseJson(1002, "用户不是总分销", null));
		}
	}
}
